﻿using System.Windows.Forms;
using System;
using System.Threading;
using System.ComponentModel;
using System.Drawing.Design;
using UtfprLibrary.Windows.Forms.ComponentModel.Security;
using UtfprLibrary.ComponentModel.Security;
using UtfprLibrary.Windows.Forms.ComponentModel;
using UtfprLibrary.ComponentModel;
using UtfprLibrary.Windows.Forms.Helpers;
using UtfprLibrary.Windows.Helpers;

namespace UtfprLibrary.Windows.Forms {
    public partial class UtfprFormLoginBase : UtfprFormBase, IUtfprModuleRestrictionService {
        #region UtfprControlService (Obsolete)

        //[Browsable(false)]
        //[Obsolete("Do not use this property: The Security behavior inherited from UtfprFormBase (IUtfprRestrictionService) is not intended to be applied in UtfprFormLoginBase logic.")]
        //new public UtfprControlBase UtfprControlBase { get; }

        [Browsable(false)]
        [Obsolete("Do not use this property: The Security behavior inherited from UtfprFormBase (IUtfprRestrictionService) is not intended to be applied in UtfprFormLoginBase logic.")]
        new public IUtfprKeyName FunctionOrOperationRestriction { get; set; }

        [Browsable(false)]
        [Obsolete("Do not use this method: The Security behavior inherited from UtfprFormBase (IUtfprRestrictionService) is not intended to be applied in UtfprFormLoginBase logic.")]
        new public bool UtfprControlVisible { get; set; }


        [Browsable(false)]
        [Obsolete("Do not use this method: The Security behavior inherited from UtfprFormBase (IUtfprRestrictionService) is not intended to be applied in UtfprFormLoginBase logic.")]
        new public bool UtfprControlEnabled { get; set; }

        #endregion        

        #region IUtfprModuleRestrictionService

        [Category("Security"), Description("Choose a system module")]
        [Editor("UtfprLibrary.Windows.Forms.ComponentModel.UtfprRestrictionEditor, UtfprLibrary.WindowsForms.Design", typeof(UITypeEditor))]
        public IUtfprKeyName Module { get; set; }

        #endregion

        public UtfprFormLoginBase() {
            if (!UtfprControlBase.IsInDesignMode) {
                if (UtfprApplication.Instance.IsSplashEnabled && UtfprApplication.Instance.IsLoginEnabled) {
                    if (!UtfprApplication.Instance.FormSplashDefault.IsDisposed) {
                        Thread splashthread = new Thread(new ThreadStart(UtfprSplashBaseHelper.ShowSplashScreen));
                        splashthread.IsBackground = true;
                        splashthread.Start();
                    }
                }
            }
            InitializeComponent();
        }

        protected virtual IUtfprUser FindUser(string username, string password) {
            throw new NotImplementedException("Override this method.");
        }

        protected virtual void Login(Control username, Control password) {
            if (UtfprLoginHelper.ValidateFields(errorProvider, username, password)) {
                var user = FindUser(username.Text, password.Text);

                if (user != null) {
                    UtfprSecurity.Instance.ConnectUser(user);
                    Close();
                    Dispose();
                }
            }
        }

        protected virtual void Logoff() {
            UtfprSecurity.Instance.DisconnectCurrentUser();
            Close();
            Dispose();
        }

        /// <summary>
        /// Passa o contexto da aplicação para o formulário principal (UtfprApplication.Instance.FormMainDefault)
        /// Application.Run(UtfprApplicationContext.Instance)
        /// Displays the FormMainDefault if InDesignMode, Modal and Visible properties are false.
        /// </summary>
        private void changeApplicationContext() {
            if (!UtfprControlBase.IsInDesignMode) {
                if (UtfprSecurity.Instance.IsUserConected) {
                    UtfprApplicationContext.Instance.MainForm = UtfprApplication.Instance.FormMainDefault;
                    UtfprApplication.Instance.FormMainDefault.Show();
                }
            }
        }

        #region EventHandlers


        private void UtfprLoginBase_FormClosing(object sender, FormClosingEventArgs e) {
            changeApplicationContext();
        }

        #endregion
    }
}
